
cd "${mystart}\Simulated data\Experiments Minorities\Experiments Data/"

* Experiments
foreach exp in 05 021 022 024 028{
foreach race in "B" "H" {
clear
cap log close
infile using "Dictionaries/Dictionaries Experiments `race'/dictionary`exp'.dct"
rename v1 ID                     //jsample
rename v2 age                    //jage
rename v3 education              //je
rename v4 productivity_type      //jfix
rename v5 latent_health          //jht
rename v6 married                //jmar
rename v7 H                      //jh
rename v8 R                      //jr
rename v9 health_shocks          //jtype2
rename v10 spouse_emp         //jtype4 - single, mar with not working spouse, mar with working spouse
rename v11 offer                 //jo
rename v12 offer_accepted        //jo_act
rename v13 trans_shock           //jzt

rename v14 human_capital         //jx_int
rename v15 human_capital_prime   //jx_int_p
rename v16 OOP                    //oop*5200
rename v17 MC                  //Charges*5200
rename v18 consumption           //jcons_act*5200 - this is the value of total HH consumption (after dividing by (1+c_tax)); for married, what goes into utility is this consumption /scaling factor 

rename v19 assets                 //jassets*5200 assets at the beginning of the period , before getting interest on it.
rename v20 assets_prime          //jas_p_act*5200 - this is the assets I take to the next period (just before the end of the period). At the beginning of next period, we add interest (after tax) to it.
rename v21 govt_transfer         //jtr_act*5200  - this is the monetary transfer received to ensure that the person can afford c_bar(1+tau_c) plus OOP 
rename v22 wage_offer            //wage_penalty(jo)*Z_INT 
rename v23 earnings          // for retirees I write the husband's SS income: pen(jhc_ret,  je)*5200  // for working age: 5200.0d0*  max(0.0d0 , ( wage_penalty(jo_act)*Z_INT * (hrs_offer(jo_act) -phi(je,jh,jtype2) )) ) -  0 if NE
rename v24 hours                 //(hrs_ft(pt) + phi(jh,jtype2))*100 when applicable 
rename v25 wage_noise            //Z_INT + error  // only for those who accept offer and work.
rename v26 spouse_income         //inc_spouse(jage,je,jh,jtype4)*5200.0d0

rename v27 tr_pay               // !jtrpay=1 	pay and get treated; jtrpay=2	don't pay, get treated; jtrpay=3	don't pay, not treated
rename v28 OOP_spouse

rename v29 COH_pay // cash on hand if pay (working age) and all retirees - NOTE THIS CAN BE NEGATIVE, THIS IS BEFORE CONSIDERING TRANSFER. IF WE WANT POST-TRANSFER, ADD THE GOV TRANSFER TO THIS.
rename v30 COH_not_pay // cash on hand if not pay (working age) - NOTE THIS CAN BE NEGATIVE
rename v31 Expenditures_pay // all expenditures other than consumption: taxes, premiums, oop.
rename v32 Expenditures_not_pay // all expenditures other than consumption: taxes, premiums, oop for wife.

rename v33 HC_65 // jhc_ret  -- earning capacity at age 64. relevant only for 65+ because determines SS
rename v34 type2_recorded // this is after considering the probability of not recording shock if not treated.
rename v35 R_recorded
rename v36 Option_treat_not_pay 
rename v37 Option_treat
rename v38 Value
gen race = "`race'"
gen experiment="`exp'"
save "Experiments Stata Files/Experiment_`race'.dta", replace
}


clear 
use "Experiments Stata Files/Experiment_B.dta"
append using "Experiments Stata Files/Experiment_H.dta"

gen race_gr=2 if race=="B"
replace race_gr=3 if race=="H"
drop race
rename race_gr race

* Marital Status
replace married=married-1 //0 = SINGLE, 1=MARRIED
label define label_mar 0 "Single" 1 "Married"
label values married label_mar
label var married "Marital Status"

* Spuse Employment and Income
label var spouse_emp "Spouse Employment"
label var spouse_income "Spouse Income"
replace spouse_emp =. if married==0
replace spouse_income =. if married==0
replace spouse_emp = spouse_emp-2 if age<65 // 0 = not working, 1=working
label define label_sw 0 "Not Working" 1 "Working"
label values spouse_emp label_sw


***AGE
label var age "Age"
replace age=age+24 

*Unique ID - within each experiment
replace ID=ID*1000+race*100+productivity_type*10+latent_health

*Health shocks
gen du=0
gen dp=0
gen s=0
replace du=1 if health_shocks>=5
replace dp=1 if health_shocks==2 | health_shocks==4 | health_shocks==6 | health_shocks==8 
replace s=1 if health_shocks==3 | health_shocks==4 | health_shocks==7 | health_shocks==8 
//drop health_shocks

*jtype2= 1 for du=1 , dp=1 , s=1 
*jtype2= 2 for du=1 , dp=2 , s=1 
*jtype2= 3 for du=1 , dp=1 , s=2 
*jtype2= 4 for du=1 , dp=2 , s=2  
*jtype2= 5  for du=2 , dp=1 , s=1 
*jtype2= 6  for du=2 , dp=2 , s=1  
*jtype2= 7  for du=2 , dp=1 , s=2 
*jtype2= 8  for du=2 , dp=2 , s=2 
gen du_record=0
gen dp_record=0
gen s_record=0
replace du_record=1 if type2_recorded>=5
replace dp_record=1 if type2_recorded==2 | type2_recorded==4 | type2_recorded==6 | type2_recorded==8 
replace s_record=1 if type2_recorded==3 | type2_recorded==4 | type2_recorded==7 | type2_recorded==8 


*Race
label define label_race 1 "White" 2 "Black" 3 "Hispanic"
label values race label_edc
label var race "Race"

*latent skill
label define label_lowhigh 1 "Low" 2 "Medium" 3 "High"
label values productivity_type label_lowhigh
label var productivity_type "Latent Skill"

*latent health
label define hlthtype 1 "Good" 2 "Bad" //1=good , 2=Bad
label values latent_health hlthtype
label var latent_health "Latent Health"

*H
label define label_gap 1 "H = Poor" 2 "H = Fair" 3 "H = Good" 
label values H label_gap
label var H "Health"

*R
label define label_lh 1 "Low Risk" 2 "High Risk"
label values R label_lh
label var R "Health Risk (R)"
label values R_recorded label_lh
label var R_recorded "Health Risk (R)"

*ESHI
gen ESHI=0 if age<65
replace ESHI=1 if  (offer_accepted==3 | offer_accepted==5) & age<65
label define label_ins 0 "No ESHI" 1 "ESHI"
label values ESHI label_ins
label var ESHI "ESHI"	

*INSURANCE 
gen INSURANCE=3
replace INSURANCE=1 if ESHI==1
replace INSURANCE=2 if govt_transfer>0 & govt_transfer!=. & health_shocks!=1 & Option_treat==2
//replace INSURANCE=3 if (govt_transfer==0 & ESHI==0) | (ESHI==0 & govt_transfer>0 &   govt_transfer!=. & Option_treat==1) // you are uninsured if you get TR but don't have option to treat
replace INSURANCE=. if age>64
label define label_ins2 1 "ESHI" 2 "Public" 3"Uninsured"
label values INSURANCE label_ins2
label var INSURANCE "Health Insurance"

drop health_shocks


* OOP, MC and pay/treat
label var OOP "Medical Treatment Cost"
label var MC "Medical Charges" 
gen I_pay = 0 if (du==1 | dp==1 | s==1) & age<65 & tr_pay!=1 // not pay
replace I_pay = 1 if (du==1 | dp==1 | s==1) & age<65 & tr_pay==1 //  pay
label var I_pay "Pays Medical Bill"
label define label_pay 0 "Does not pay" 1 "Pays"
label values I_pay label_pay
gen I_treat = 0 if (du==1 | dp==1 | s==1) & age<65 & tr_pay==3 // not treat
replace I_treat = 1 if (du==1 | dp==1 | s==1) & age<65 & tr_pay!=3 //  treat
label var I_treat "Treat Health Shocks"
label define label_treat 0 "Not treat" 1 "Treat"
label values I_treat label_treat
replace tr_pay=0 if (du==0 & dp==0 &  s==0)
label define label_trpay 0 "No Shock" 1 "Treat and Pay" 2 "Treat and Not Pay" 3 "Not Treat and Not Pay"
label values tr_pay label_trpay
label var tr_pay "Treat/Pay"
gen MC_orig=MC
label var MC_orig "Medical Charges"
replace MC=0 if I_treat == 0 | (ESHI!=1 & du==0 & dp==0 & s==0) // if you don't get treated, no charges, to be similar with data. - and if no ESHI and no shocks, then no charges.
//replace OOP=0 if I_treat == 0 | I_pay == 0
label var Option_treat_not_pay "Option to treat & not pay"
label define label_option 1 "Has to Pay" 2 "Does not have to pay" 
label values Option_treat_not_pay label_option

label var Option_treat "Treatment Possible"
replace Option_treat=. if Option_treat==0 // retirees
//replace Option_treat=. if Option_treat_not_pay!=1 // this variable is conditional on not having to treat
replace Option_treat=.  if (du==0 & dp==0 &  s==0)  // this variable is conditional on shock
replace Option_treat=.  if ESHI==1 // this variable is conditional on being uninsured
replace Option_treat=0 if Option_treat==1 // you don't have option to treat
replace Option_treat=1 if Option_treat==2 // you have option to treat
label define label_option1 0 "No" 1 "Yes" 
label values Option_treat label_option1

gen Option_tr_pay= 0
replace Option_tr_pay=1 if Option_treat==1 & Option_treat_not_pay==1 // can treat but must pay
replace Option_tr_pay=2 if Option_treat==1 & Option_treat_not_pay==2 // can treat and doesn't have to pay
replace Option_tr_pay=3 if Option_treat==0  // cannot treat
label define label_trpay_opt 0 "No Shock" 1 "Can Treat but Must Pay" 2 "Can Treat and Not Pay" 3 "Cannot Treat"
label values Option_tr_pay label_trpay_opt
label var Option_tr_pay "Treat and Pay options"

//tr_pay - version where we add catagory where you "pay" but it's because of Medicaid (cons floor)
gen tr_pay_alt = tr_pay
replace tr_pay_alt=4 if tr_pay == 3
replace tr_pay_alt=3 if tr_pay == 2
replace tr_pay_alt=2 if tr_pay == 1 & govt_transfer>0 & govt_transfer!=.

label define label_trpay_alt 0 "No Shock" 1 "Treat and Pay OOP" 2 "Treat and Pay Mcaid" 3 "Treat and Not Pay" 4 "Not Treat and Not Pay"
label values tr_pay_alt label_trpay_alt
label var tr_pay_alt "Treat/Pay/Mcaid"

* EMP Offer
gen employment_offer_yn=0 if offer==1
replace employment_offer_yn=1 if offer>=2 & offer<=5
label var employment_offer_yn "Has Emp Offer"
label define label_eo 0 "No Offer" 1 "Has Offer"
label values employment_offer_yn label_eo

*EMP
* need to reclassify people based on actual number of hours worked to be consistent with data. 
* so if you have many sick days, you may only be PT not FT, or even Not Employed
gen emp =offer_accepted
replace emp=1 if emp>1 & hours<10
replace emp=2 if emp==4 & hours>=10 & hours<28.85	
replace emp=3 if emp==5 & hours>=10 & hours<28.85 
label var emp "Employment Status"
label define label_emps 1 "Not Emp" 2 "PT, no ESHI" 3 "PT, ESHI" 4 "FT, no ESHI" 5 "FT, ESHI"
label values emp label_emps
**
gen emp_ft_pt=0 if emp==1
replace emp_ft_pt=1 if emp==2 | emp==3
replace emp_ft_pt=2 if emp==4 | emp==5
label var emp_ft_pt "Employment Status"
label define label_emps_1 0 "Not Emp" 1 "PT" 2 "FT"
label values emp_ft_pt label_emps_1
**
gen employed_yn=0 if age<=64
replace employed_yn=1 if emp>=2 & emp<=5
label var employed_yn "Employed"
label define label_emp 0 "Not Emp" 1 "Employed"
label values employed_yn label_emp
	
*Wage offer
replace wage_offer=. if offer==1
label var wage_offer "Wage offer"

*Sick Days - defined only if working. 
gen sick_days = 0 if  offer_accepted>=2 & offer_accepted<=5
replace sick_days = 20-hours if (offer_accepted==2 | offer_accepted==3) 
replace sick_days = 40-hours if (offer_accepted==4 | offer_accepted==5) 
* convert to FT days per year
replace sick_days= sick_days *52/8
label var sick_days "Sick days per yr"

*Type2
gen type2=1 if du==0 & dp==0 & s==0
 replace type2=2 if du==0 & dp==1 & s==0
  replace type2=3 if du==0 & dp==0 & s==1
   replace type2=4 if du==0 & dp==1 & s==1
    replace type2=5 if du==1 & dp==0 & s==0
	 replace type2=6 if du==1 & dp==1 & s==0
	  replace type2=7 if du==1 & dp==0 & s==1
	   replace type2=8 if du==1 & dp==1 & s==1
	
* SS income of the husband
gen SS_income = earnings if age>=65
replace earnings=. if age>=65
* inc_spouse is the SS of the wife at ages 65+

* GOV TRANSFER INDICATOR
		gen TR=0 if  govt_transfer==0 
		replace TR=1 if govt_transfer>0
		label var TR "Gov Transfer"
		label define label_tr01 0 "No Gov Tr" 1 "Has Gov Tr"
label values TR label_tr01

* Income quintiles
gen income=earnings + assets*(0.045)
xtile inc_quintile=income if age<65 ,n(5)
label var inc_quintile "Income Quintile"
label define inc_q 1 "1st" 2 "2nd" 3 "3rd" 4 "4th" 5 "5th"
label values inc_quintile inc_q
label var income "Income"

* spouse status
gen Spouse_status = 1 if married==0 
replace Spouse_status = 2 if married==1 & spouse_emp==0
replace Spouse_status = 3 if married==1 & spouse_emp==1
label define sp_stat 1 "Single" 2 "Married, Spouse N.E." 3 "Married, Spouse E."
label values Spouse_status sp_stat
label var Spouse_status "Marital and Spouse Status"

* AGE GROUPS
gen age_group=27  if age>=25 & age<30
replace age_group=32  if age>=30 & age<35
replace age_group=37  if age>=35 & age<40
replace age_group=42  if age>=40 & age<45
replace age_group=47  if age>=45 & age<50
replace age_group=52  if age>=50 & age<55
replace age_group=57  if age>=55 & age<60
replace age_group=62  if age>=60 & age<65
replace age_group=67  if age>=65 & age<70
replace age_group=72  if age>=70 & age<75
replace age_group=77  if age>=75 & age<80
replace age_group=82  if age>=80 & age<85
replace age_group=87  if age>=85 & age<90
replace age_group=92  if age>=90 & age<95
replace age_group=97  if age>=95 & age<101
label var age_group "Age Group"

* human capital at 65 groups 
*gen HC_group = 1 if age==64 & human_capital<=23
*replace HC_group = 2 if age==64 & human_capital>23 & human_capital<=28
*replace HC_group = 3 if age==64 & human_capital>28 //& human_capital<=42
//replace HC_group = 4 if age==64 & human_capital>42 
*sort ID
*by ID: egen HC_group_model=max(HC_group)
label var HC_65 "Human Capital at Age 65 Group"
//drop HC_65 HC_group


sort ID age
gen I_death=0
replace I_death=1 if ID!=ID[_n+1]

gen age_sq=age^2
gen age_cub=age^3

destring experiment, replace
order experiment
sort experiment ID age
save "Experiments Stata Files/Experiment`exp'.dta", replace
}



*Counterfactuals
foreach exp in 4 14  {
foreach race in "B" "H" {
clear
cap log close
infile using "Dictionaries/Dictionaries Counterfactuals `race'/dictionary`exp'.dct"
rename v1 ID                     //jsample
rename v2 age                    //jage
rename v3 education              //je
rename v4 productivity_type      //jfix
rename v5 latent_health          //jht
rename v6 married                //jmar
rename v7 H                      //jh
rename v8 R                      //jr
rename v9 health_shocks          //jtype2
rename v10 spouse_emp         //jtype4 - single, mar with not working spouse, mar with working spouse
rename v11 offer                 //jo
rename v12 offer_accepted        //jo_act
rename v13 trans_shock           //jzt

rename v14 human_capital         //jx_int
rename v15 human_capital_prime   //jx_int_p
rename v16 OOP                    //oop*5200
rename v17 MC                  //Charges*5200
rename v18 consumption           //jcons_act*5200 - this is the value of total HH consumption (after dividing by (1+c_tax)); for married, what goes into utility is this consumption /scaling factor 

rename v19 assets                 //jassets*5200 assets at the beginning of the period , before getting interest on it.
rename v20 assets_prime          //jas_p_act*5200 - this is the assets I take to the next period (just before the end of the period). At the beginning of next period, we add interest (after tax) to it.
rename v21 govt_transfer         //jtr_act*5200  - this is the monetary transfer received to ensure that the person can afford c_bar(1+tau_c) plus OOP 
rename v22 wage_offer            //wage_penalty(jo)*Z_INT 
rename v23 earnings          // for retirees I write the husband's SS income: pen(jhc_ret,  je)*5200  // for working age: 5200.0d0*  max(0.0d0 , ( wage_penalty(jo_act)*Z_INT * (hrs_offer(jo_act) -phi(je,jh,jtype2) )) ) -  0 if NE
rename v24 hours                 //(hrs_ft(pt) + phi(jh,jtype2))*100 when applicable 
rename v25 wage_noise            //Z_INT + error  // only for those who accept offer and work.
rename v26 spouse_income         //inc_spouse(jage,je,jh,jtype4)*5200.0d0

rename v27 tr_pay               // !jtrpay=1 	pay and get treated; jtrpay=2	don't pay, get treated; jtrpay=3	don't pay, not treated
rename v28 OOP_spouse

rename v29 COH_pay // cash on hand if pay (working age) and all retirees - NOTE THIS CAN BE NEGATIVE, THIS IS BEFORE CONSIDERING TRANSFER. IF WE WANT POST-TRANSFER, ADD THE GOV TRANSFER TO THIS.
rename v30 COH_not_pay // cash on hand if not pay (working age) - NOTE THIS CAN BE NEGATIVE
rename v31 Expenditures_pay // all expenditures other than consumption: taxes, premiums, oop.
rename v32 Expenditures_not_pay // all expenditures other than consumption: taxes, premiums, oop for wife.

rename v33 HC_65 // jhc_ret  -- earning capacity at age 64. relevant only for 65+ because determines SS
rename v34 type2_recorded // this is after considering the probability of not recording shock if not treated.
rename v35 R_recorded
rename v36 Option_treat_not_pay 
rename v37 Option_treat
rename v38 Value
rename v39 Utility
rename v40 Value_death
gen race = "`race'"
gen experiment="`exp'"
save "Counterfactuals Stata Files/Counterfactual`race'.dta", replace
}


clear 
use "Counterfactuals Stata Files/CounterfactualB.dta"
append using "Counterfactuals Stata Files/CounterfactualH.dta"


gen race_gr=2 if race=="B"
replace race_gr=3 if race=="H"
drop race
rename race_gr race

* Marital Status
replace married=married-1 //0 = SINGLE, 1=MARRIED
label define label_mar 0 "Single" 1 "Married"
label values married label_mar
label var married "Marital Status"

* Spuse Employment and Income
label var spouse_emp "Spouse Employment"
label var spouse_income "Spouse Income"
replace spouse_emp =. if married==0
replace spouse_income =. if married==0
replace spouse_emp = spouse_emp-2 if age<65 // 0 = not working, 1=working
label define label_sw 0 "Not Working" 1 "Working"
label values spouse_emp label_sw


***AGE
label var age "Age"
replace age=age+24 

*Unique ID - within each experiment
replace ID=ID*1000+race*100+productivity_type*10+latent_health

*Health shocks
gen du=0
gen dp=0
gen s=0
replace du=1 if health_shocks>=5
replace dp=1 if health_shocks==2 | health_shocks==4 | health_shocks==6 | health_shocks==8 
replace s=1 if health_shocks==3 | health_shocks==4 | health_shocks==7 | health_shocks==8 
//drop health_shocks

*jtype2= 1 for du=1 , dp=1 , s=1 
*jtype2= 2 for du=1 , dp=2 , s=1 
*jtype2= 3 for du=1 , dp=1 , s=2 
*jtype2= 4 for du=1 , dp=2 , s=2  
*jtype2= 5  for du=2 , dp=1 , s=1 
*jtype2= 6  for du=2 , dp=2 , s=1  
*jtype2= 7  for du=2 , dp=1 , s=2 
*jtype2= 8  for du=2 , dp=2 , s=2 
gen du_record=0
gen dp_record=0
gen s_record=0
replace du_record=1 if type2_recorded>=5
replace dp_record=1 if type2_recorded==2 | type2_recorded==4 | type2_recorded==6 | type2_recorded==8 
replace s_record=1 if type2_recorded==3 | type2_recorded==4 | type2_recorded==7 | type2_recorded==8 


*Race
label define label_race 1 "White" 2 "Black" 3 "Hispanic"
label values race label_edc
label var race "Race"

*latent skill
label define label_lowhigh 1 "Low" 2 "Medium" 3 "High"
label values productivity_type label_lowhigh
label var productivity_type "Latent Skill"

*latent health
label define hlthtype 1 "Good" 2 "Bad" //1=good , 2=Bad
label values latent_health hlthtype
label var latent_health "Latent Health"

*H
label define label_gap 1 "H = Poor" 2 "H = Fair" 3 "H = Good" 
label values H label_gap
label var H "Health"

*R
label define label_lh 1 "Low Risk" 2 "High Risk"
label values R label_lh
label var R "Health Risk (R)"
label values R_recorded label_lh
label var R_recorded "Health Risk (R)"

*ESHI
gen ESHI=0 if age<65
replace ESHI=1 if  (offer_accepted==3 | offer_accepted==5) & age<65
label define label_ins 0 "No ESHI" 1 "ESHI"
label values ESHI label_ins
label var ESHI "ESHI"	

*INSURANCE 
gen INSURANCE=3
replace INSURANCE=1 if ESHI==1
replace INSURANCE=2 if govt_transfer>0 & govt_transfer!=. & health_shocks!=1 & Option_treat==2
//replace INSURANCE=3 if (govt_transfer==0 & ESHI==0) | (ESHI==0 & govt_transfer>0 &   govt_transfer!=. & Option_treat==1) // you are uninsured if you get TR but don't have option to treat
replace INSURANCE=. if age>64
label define label_ins2 1 "ESHI" 2 "Public" 3"Uninsured"
label values INSURANCE label_ins2
label var INSURANCE "Health Insurance"

drop health_shocks


* OOP, MC and pay/treat
label var OOP "Medical Treatment Cost"
label var MC "Medical Charges" 
gen I_pay = 0 if (du==1 | dp==1 | s==1) & age<65 & tr_pay!=1 // not pay
replace I_pay = 1 if (du==1 | dp==1 | s==1) & age<65 & tr_pay==1 //  pay
label var I_pay "Pays Medical Bill"
label define label_pay 0 "Does not pay" 1 "Pays"
label values I_pay label_pay
gen I_treat = 0 if (du==1 | dp==1 | s==1) & age<65 & tr_pay==3 // not treat
replace I_treat = 1 if (du==1 | dp==1 | s==1) & age<65 & tr_pay!=3 //  treat
label var I_treat "Treat Health Shocks"
label define label_treat 0 "Not treat" 1 "Treat"
label values I_treat label_treat
replace tr_pay=0 if (du==0 & dp==0 &  s==0)
label define label_trpay 0 "No Shock" 1 "Treat and Pay" 2 "Treat and Not Pay" 3 "Not Treat and Not Pay"
label values tr_pay label_trpay
label var tr_pay "Treat/Pay"
gen MC_orig=MC
label var MC_orig "Medical Charges"
replace MC=0 if I_treat == 0 | (ESHI!=1 & du==0 & dp==0 & s==0) // if you don't get treated, no charges, to be similar with data. - and if no ESHI and no shocks, then no charges.
//replace OOP=0 if I_treat == 0 | I_pay == 0
label var Option_treat_not_pay "Option to treat & not pay"
label define label_option 1 "Has to Pay" 2 "Does not have to pay" 
label values Option_treat_not_pay label_option

label var Option_treat "Treatment Possible"
replace Option_treat=. if Option_treat==0 // retirees
//replace Option_treat=. if Option_treat_not_pay!=1 // this variable is conditional on not having to treat
replace Option_treat=.  if (du==0 & dp==0 &  s==0)  // this variable is conditional on shock
replace Option_treat=.  if ESHI==1 // this variable is conditional on being uninsured
replace Option_treat=0 if Option_treat==1 // you don't have option to treat
replace Option_treat=1 if Option_treat==2 // you have option to treat
label define label_option1 0 "No" 1 "Yes" 
label values Option_treat label_option1

gen Option_tr_pay= 0
replace Option_tr_pay=1 if Option_treat==1 & Option_treat_not_pay==1 // can treat but must pay
replace Option_tr_pay=2 if Option_treat==1 & Option_treat_not_pay==2 // can treat and doesn't have to pay
replace Option_tr_pay=3 if Option_treat==0  // cannot treat
label define label_trpay_opt 0 "No Shock" 1 "Can Treat but Must Pay" 2 "Can Treat and Not Pay" 3 "Cannot Treat"
label values Option_tr_pay label_trpay_opt
label var Option_tr_pay "Treat and Pay options"

//tr_pay - version where we add catagory where you "pay" but it's because of Medicaid (cons floor)
gen tr_pay_alt = tr_pay
replace tr_pay_alt=4 if tr_pay == 3
replace tr_pay_alt=3 if tr_pay == 2
replace tr_pay_alt=2 if tr_pay == 1 & govt_transfer>0 & govt_transfer!=.

label define label_trpay_alt 0 "No Shock" 1 "Treat and Pay OOP" 2 "Treat and Pay Mcaid" 3 "Treat and Not Pay" 4 "Not Treat and Not Pay"
label values tr_pay_alt label_trpay_alt
label var tr_pay_alt "Treat/Pay/Mcaid"

* EMP Offer
gen employment_offer_yn=0 if offer==1
replace employment_offer_yn=1 if offer>=2 & offer<=5
label var employment_offer_yn "Has Emp Offer"
label define label_eo 0 "No Offer" 1 "Has Offer"
label values employment_offer_yn label_eo

*EMP
* need to reclassify people based on actual number of hours worked to be consistent with data. 
* so if you have many sick days, you may only be PT not FT, or even Not Employed
gen emp =offer_accepted
replace emp=1 if emp>1 & hours<10
replace emp=2 if emp==4 & hours>=10 & hours<28.85	
replace emp=3 if emp==5 & hours>=10 & hours<28.85 
label var emp "Employment Status"
label define label_emps 1 "Not Emp" 2 "PT, no ESHI" 3 "PT, ESHI" 4 "FT, no ESHI" 5 "FT, ESHI"
label values emp label_emps
**
gen emp_ft_pt=0 if emp==1
replace emp_ft_pt=1 if emp==2 | emp==3
replace emp_ft_pt=2 if emp==4 | emp==5
label var emp_ft_pt "Employment Status"
label define label_emps_1 0 "Not Emp" 1 "PT" 2 "FT"
label values emp_ft_pt label_emps_1
**
gen employed_yn=0 if age<=64
replace employed_yn=1 if emp>=2 & emp<=5
label var employed_yn "Employed"
label define label_emp 0 "Not Emp" 1 "Employed"
label values employed_yn label_emp
	
*Wage offer
replace wage_offer=. if offer==1
label var wage_offer "Wage offer"

*Sick Days
gen sick_days = 0 if  offer_accepted>=2 & offer_accepted<=5
replace sick_days = 20-hours if (offer_accepted==2 | offer_accepted==3) 
replace sick_days = 40-hours if (offer_accepted==4 | offer_accepted==5) 
* convert to FT days per year
replace sick_days= sick_days *52/8
label var sick_days "Sick days per yr"

*Type2
gen type2=1 if du==0 & dp==0 & s==0
 replace type2=2 if du==0 & dp==1 & s==0
  replace type2=3 if du==0 & dp==0 & s==1
   replace type2=4 if du==0 & dp==1 & s==1
    replace type2=5 if du==1 & dp==0 & s==0
	 replace type2=6 if du==1 & dp==1 & s==0
	  replace type2=7 if du==1 & dp==0 & s==1
	   replace type2=8 if du==1 & dp==1 & s==1
	
* SS income of the husband
gen SS_income = earnings if age>=65
replace earnings=. if age>=65
* inc_spouse is the SS of the wife at ages 65+

* GOV TRANSFER INDICATOR
		gen TR=0 if  govt_transfer==0 
		replace TR=1 if govt_transfer>0
		label var TR "Gov Transfer"
		label define label_tr01 0 "No Gov Tr" 1 "Has Gov Tr"
label values TR label_tr01

* Income quintiles
gen income=earnings + assets*(0.045)
xtile inc_quintile=income if age<65 ,n(5)
label var inc_quintile "Income Quintile"
label define inc_q 1 "1st" 2 "2nd" 3 "3rd" 4 "4th" 5 "5th"
label values inc_quintile inc_q
label var income "Income"

* spouse status
gen Spouse_status = 1 if married==0 
replace Spouse_status = 2 if married==1 & spouse_emp==0
replace Spouse_status = 3 if married==1 & spouse_emp==1
label define sp_stat 1 "Single" 2 "Married, Spouse N.E." 3 "Married, Spouse E."
label values Spouse_status sp_stat
label var Spouse_status "Marital and Spouse Status"

* AGE GROUPS
gen age_group=27  if age>=25 & age<30
replace age_group=32  if age>=30 & age<35
replace age_group=37  if age>=35 & age<40
replace age_group=42  if age>=40 & age<45
replace age_group=47  if age>=45 & age<50
replace age_group=52  if age>=50 & age<55
replace age_group=57  if age>=55 & age<60
replace age_group=62  if age>=60 & age<65
replace age_group=67  if age>=65 & age<70
replace age_group=72  if age>=70 & age<75
replace age_group=77  if age>=75 & age<80
replace age_group=82  if age>=80 & age<85
replace age_group=87  if age>=85 & age<90
replace age_group=92  if age>=90 & age<95
replace age_group=97  if age>=95 & age<101
label var age_group "Age Group"

* human capital at 65 groups 
*gen HC_group = 1 if age==64 & human_capital<=23
*replace HC_group = 2 if age==64 & human_capital>23 & human_capital<=28
*replace HC_group = 3 if age==64 & human_capital>28 //& human_capital<=42
//replace HC_group = 4 if age==64 & human_capital>42 
*sort ID
*by ID: egen HC_group_model=max(HC_group)
label var HC_65 "Human Capital at Age 65 Group"
//drop HC_65 HC_group


sort ID age
gen I_death=0
replace I_death=1 if ID!=ID[_n+1]

gen age_sq=age^2
gen age_cub=age^3

destring experiment, replace
order experiment
sort experiment ID age
save "Counterfactuals Stata Files/Counterfactual`exp'.dta", replace
}

